//
// Created by zhangrunquan on 20-4-15.
//

#ifndef LEVELDB_TEST_DATA_H
#define LEVELDB_TEST_DATA_H

#include "leveldb/db.h"
#include <cstdio>
#include <iostream>
#include <stdio.h>
#include <fstream>


using namespace std;
using namespace leveldb;

void putRecord(long length,DB *db,const string& key){
    char *ptr=new char[length+1]; // +1 for '\0'
    for (int i = 0; i < length; ++i) {
        *(ptr+i)='1';
    }
    *(ptr+length)='\0';
//    cout<<ptr<<"\n";

    db->Put(WriteOptions(),key,ptr);
    delete[](ptr);
}

DB* openDB(const string& name){
    DB* db = nullptr;
    Options op;
    op.create_if_missing = true;
    Status status = DB::Open(op, name, &db);
    assert(status.ok());
    return db;
}


void makeTestData(){
    const int RECORDlEN=32768;
    //keylength默认保持1
    //则单block情况，除value和vlength外共22byte
    int valueLength;

    //test1
    valueLength=RECORDlEN+10;
    DB *db1=openDB("testdb1");
    putRecord(valueLength,db1,"1");
    delete db1;

    //test2
    valueLength=2*RECORDlEN+10;
    DB *db2=openDB("testdb2");
    putRecord(valueLength,db2,"1");
    delete db2;


    //test 3
    //header在上一block
    valueLength=RECORDlEN-25-7;
    DB *db3=openDB("testdb3");
    putRecord(valueLength,db3,"1");
    putRecord(20,db3,"2");
    delete db3;

    //test 4
    //vlength跨block
    int firstRecordLength=RECORDlEN-22-1;
    int firstValueLength=firstRecordLength-25;
    DB * db4=openDB("testdb4");
    putRecord(firstValueLength,db4,"1");
    valueLength=257;
    putRecord(valueLength,db4,"2");
    delete db4;

    //test 5
    //block剩6字节
    valueLength=RECORDlEN-25-6;
    DB *db5=openDB("testdb5");
    putRecord(valueLength,db5,"1");
    valueLength=257;
    putRecord(valueLength,db5,"2");
    delete db5;
}


#endif //LEVELDB_TEST_DATA_H
